'''
Created on 2012-4-12

@author: Administrator
'''
from popcan.helper.loggerhelper import Module,QueueLogger
from popcan.core.exceptions import DBAccessError,MemAccessError,RedisAccessError
from popcan.views.errors import dbaccess,memaccess,redisaccess,unknownaccess
from popcan.helper.picklehelper import YamlHelper
import traceback

class ExceptionMiddleware(object):
    
    def __init__(self):
        self.pickle = YamlHelper()
    
    def process_exception(self,request,exception):
        QueueLogger().error(Module.Middleware, 'error happend!request=%s' % self.pickle.dump(request),traceback.format_exc() )
        if isinstance(exception,DBAccessError):
            return dbaccess(request)
        elif isinstance(exception,MemAccessError):
            return memaccess(request)
        elif isinstance(exception,RedisAccessError):
            return redisaccess(request)
        else:
            return unknownaccess(request)